package cn.ffcs.bpm.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpm.model.TransactProcessStatus;

/**
 * 
 * 流程DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class TransactProcessStatusDAO<D extends TransactProcessStatus> extends
        BaseDAO<D> implements ITransactProcessStatusDAO<D>
{

    public String getEntityName()
    {
        return new TransactProcessStatus().getClass().getName();
    }

    public void remove(Object entity, String fParam, String fParamValue)
    {
        String hql = "delete from " + getEntityName() + " b where b." + fParam
                + " = '" + fParamValue + "'";
        getSession().createQuery(hql).executeUpdate();
    }

    /**
     * 按查询条件获取总数 重写
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<D> findBy(Criterion cond)
    {
        final Criterion conds = cond;
        return getHibernateTemplate().executeFind(new HibernateCallback()
        {
            public List doInHibernate(Session session)
                    throws HibernateException
            {
                Criteria criteria = session.createCriteria(getEntityName());
                criteria.add(conds);
                return criteria.list();
            }
        });
    }
    

    /**
     * 按条件查询翻页
     * @param cond
     * @param currPage
     * @param pageSize
     * @return
     */
    @SuppressWarnings("unchecked")
    public PageControl findByPage(Criterion cond, int currPage, int pageSize)
    {
        PageControl pageControl = new PageControl(findBy(cond).size(),
                currPage, pageSize);

        final int startRow = pageControl.getStart();
        final int endRow = pageControl.getPageSize();
        final Criterion conds = cond;

        List<D> results = getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public List doInHibernate(Session session)
                            throws HibernateException
                    {
                        Criteria criteria = session
                                .createCriteria(getEntityName());
                        criteria.add(conds);
                        // 设置翻页数据
                        criteria.setFirstResult(startRow);
                        criteria.setMaxResults(endRow);

                        return criteria.list();
                    }
                });

        return new PageControl(results, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
    }
}
